package com.empire.module.member.controller.admin.personwarningsummary;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.personwarningsummary.vo.*;
import com.empire.module.member.dal.dataobject.personwarningsummary.PersonWarningSummaryDO;
import com.empire.module.member.service.personwarningsummary.PersonWarningSummaryService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 人员预警统计汇总")
@RestController
@RequestMapping("/member/person-warning-summary")
@Validated
public class PersonWarningSummaryController {

    @Resource
    private PersonWarningSummaryService personWarningSummaryService;

    @PostMapping("/create")
    @Operation(summary = "创建人员预警统计汇总")
    @PreAuthorize("@ss.hasPermission('member:person-warning-summary:create')")
    public CommonResult<Long> createPersonWarningSummary(@Valid @RequestBody PersonWarningSummarySaveReqVO createReqVO) {
        return success(personWarningSummaryService.createPersonWarningSummary(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新人员预警统计汇总")
    @PreAuthorize("@ss.hasPermission('member:person-warning-summary:update')")
    public CommonResult<Boolean> updatePersonWarningSummary(@Valid @RequestBody PersonWarningSummarySaveReqVO updateReqVO) {
        personWarningSummaryService.updatePersonWarningSummary(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除人员预警统计汇总")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:person-warning-summary:delete')")
    public CommonResult<Boolean> deletePersonWarningSummary(@RequestParam("id") Long id) {
        personWarningSummaryService.deletePersonWarningSummary(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得人员预警统计汇总")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:person-warning-summary:query')")
    public CommonResult<PersonWarningSummaryRespVO> getPersonWarningSummary(@RequestParam("id") Long id) {
        PersonWarningSummaryDO personWarningSummary = personWarningSummaryService.getPersonWarningSummary(id);
        return success(BeanUtils.toBean(personWarningSummary, PersonWarningSummaryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得人员预警统计汇总分页")
    @PreAuthorize("@ss.hasPermission('member:person-warning-summary:query')")
    public CommonResult<PageResult<PersonWarningSummaryRespVO>> getPersonWarningSummaryPage(@Valid PersonWarningSummaryPageReqVO pageReqVO) {
        PageResult<PersonWarningSummaryDO> pageResult = personWarningSummaryService.getPersonWarningSummaryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PersonWarningSummaryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出人员预警统计汇总 Excel")
    @PreAuthorize("@ss.hasPermission('member:person-warning-summary:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPersonWarningSummaryExcel(@Valid PersonWarningSummaryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PersonWarningSummaryDO> list = personWarningSummaryService.getPersonWarningSummaryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "人员预警统计汇总.xls", "数据", PersonWarningSummaryRespVO.class,
                        BeanUtils.toBean(list, PersonWarningSummaryRespVO.class));
    }

}